package com.fyl.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.fyl.anno.MyMapperScan;
import com.fyl.beans.OtherBean;
import com.fyl.imports.MyImportBeanDefinitionRegistrar;
import com.fyl.imports.MyImportSelector;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration
//<context:component-scan base-package="com.fyl"></context:component-scan>
@ComponentScan({"com.fyl"})
//<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
@PropertySource({"classpath:jdbc.properties"})
//@Import(OtherBean.class)
//@Import(MyImportSelector.class)
//@Import(MyImportBeanDefinitionRegistrar.class)
@MyMapperScan
@MapperScan("com.fyl.mapper")
public class SpringConfig {

    @Bean
    public DataSource dataSource(@Value("${jdbc.driver}") String driver,
                                 @Value("${jdbc.url}") String url,
                                 @Value("${jdbc.username}") String username,
                                 @Value("${jdbc.password}") String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
}
